package com.huihuiyun.huihuiyun.server.controller.seller.website;

import com.huihuiyun.huihuiyun.server.controller.seller.website.authorize.SellerWebsiteAuthorizeHandler;
import com.huihuiyun.huihuiyun.server.controller.seller.website.viewobjects.HhyUniteMallProductVO;
import com.huihuiyun.huihuiyun.server.domain.HhyProduct;
import com.huihuiyun.huihuiyun.server.domain.HhySeller;
import com.huihuiyun.huihuiyun.server.service.HhyProductService;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVO;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path(Const.context)
@Component
@Slf4j
public class SellerWebsiteUniteMallProductController {
    @Autowired
    private SellerWebsiteAuthorizeHandler sellerWebsiteAuthorizeHandler;

    @Autowired
    private HhyProductService hhyUniteMallProductService;

    @Context
    private HttpServletRequest httpServletRequest;

    @Context
    private HttpServletResponse httpServletResponse;

    /**
     * 是否有权限
     */
    private static boolean hasRight(HhySeller hhySeller, HhyProduct hhyUniteMallProduct) {
        return StringUtils.equals(hhyUniteMallProduct.sellerId, hhySeller.id);
    }

    @POST
    @Path("/uniteMallProduct/add")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO add(HhyUniteMallProductVO hhyUniteMallProductVO) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            // 业务
            HhyProduct hhyUniteMallProduct = ResultVOUtil.convert(hhyUniteMallProductVO, HhyProduct.class);
            hhyUniteMallProduct.sellerId = hhySeller.id;
            hhyUniteMallProduct = hhyUniteMallProductService.put(hhyUniteMallProduct);
            hhyUniteMallProductVO = ResultVOUtil.convert(hhyUniteMallProduct, HhyUniteMallProductVO.class);
            return ResultVOUtil.success(hhyUniteMallProductVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("创建失败，请稍后重试");
        }
    }

    @POST
    @Path("/uniteMallProduct/deleteOne")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO deleteOne(HhyUniteMallProductVO hhyUniteMallProductVO) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            String id = hhyUniteMallProductVO.id;
            HhyProduct hhyUniteMallProduct = hhyUniteMallProductService.findOne(id);
            if (!hasRight(hhySeller, hhyUniteMallProduct)) {
                return ResultVOUtil.error("没有权限");
            }
            // 业务
            hhyUniteMallProductService.delete(id);
            return ResultVOUtil.success();
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("删除失败，请稍后重试");
        }
    }

    @POST
    @Path("/uniteMallProduct/deleteAll")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO deleteAll(List<HhyUniteMallProductVO> hhyUniteMallProductVOList) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            for (HhyUniteMallProductVO hhyUniteMallProductVO : hhyUniteMallProductVOList) {
                String id = hhyUniteMallProductVO.id;
                HhyProduct hhyUniteMallProduct = hhyUniteMallProductService.findOne(id);
                if (!hasRight(hhySeller, hhyUniteMallProduct)) {
                    return ResultVOUtil.error("没有权限");
                }
            }

            List<HhyProduct> hhyUniteMallProductList = ResultVOUtil.convertList(hhyUniteMallProductVOList, HhyProduct.class);
            // 业务
            for (HhyProduct hhyUniteMallProduct : hhyUniteMallProductList) {
                hhyUniteMallProductService.delete(hhyUniteMallProduct.id);
            }
            return ResultVOUtil.success();
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("删除失败，请稍后重试");
        }
    }

    @POST
    @Path("/uniteMallProduct/update")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO updateUniteMallProduct(HhyUniteMallProductVO hhyUniteMallProductVO) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            String id = hhyUniteMallProductVO.id;
            HhyProduct hhyUniteMallProduct = hhyUniteMallProductService.findOne(id);
            if (!hasRight(hhySeller, hhyUniteMallProduct)) {
                return ResultVOUtil.error("没有权限");
            }
            // 业务
            hhyUniteMallProduct = ResultVOUtil.copyProperties(hhyUniteMallProductVO, hhyUniteMallProduct);
            hhyUniteMallProduct = hhyUniteMallProductService.put(hhyUniteMallProduct);
            hhyUniteMallProductVO = ResultVOUtil.convert(hhyUniteMallProduct, HhyUniteMallProductVO.class);
            return ResultVOUtil.success(hhyUniteMallProductVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("更新失败，请稍后重试");
        }
    }

    @GET
    @Path("/uniteMallProduct/findOne")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO<HhyUniteMallProductVO> findUniteMallProduct(@QueryParam("id") String id) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            HhyProduct hhyUniteMallProduct = hhyUniteMallProductService.findOne(id);
            if (!hasRight(hhySeller, hhyUniteMallProduct)) {
                return ResultVOUtil.error("没有权限");
            }
            // 业务
            HhyUniteMallProductVO hhyUniteMallProductVO = ResultVOUtil.convert(hhyUniteMallProduct, HhyUniteMallProductVO.class);
            return ResultVOUtil.success(hhyUniteMallProductVO);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("获取数据失败，请稍后重试");
        }
    }

    @GET
    @Path("/uniteMallProduct/findAll")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO findAllUniteMallProduct(@QueryParam("currentPage") @DefaultValue("1") int currentPage, @QueryParam("pageSize") @DefaultValue("10") int pageSize) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            // 业务
            String sellerId = hhySeller.id;
            int mallType = HhyProduct.mallTypeUniteMall;
            currentPage = ResultVOUtil.getCurrentPageFromAntDesignProQueryParam(currentPage);
            Page<HhyProduct> uniteMallProductPage = hhyUniteMallProductService.findAllBySellerIdAndMallType(sellerId, mallType, currentPage, pageSize);
            Object data = ResultVOUtil.convertJpaPage2AntDesignTableData(uniteMallProductPage, HhyUniteMallProductVO.class);
            return ResultVOUtil.success(data);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error("获取数据失败，请稍后重试");
        }
    }

    @POST
    @Path("/uniteMallProduct/up")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO upProduct(HhyUniteMallProductVO hhyProductVO) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            String productId = hhyProductVO.id;
            HhyProduct hhyUniteMallProduct = hhyUniteMallProductService.findOne(productId);
            if (!hasRight(hhySeller, hhyUniteMallProduct)) {
                return ResultVOUtil.error("不能上架其他商家的商品");
            }
            // 业务
            hhyUniteMallProduct.status = HhyProduct.statusUp;
            hhyUniteMallProduct = hhyUniteMallProductService.put(hhyUniteMallProduct);
            HhyUniteMallProductVO hhyProductVOOutput = ResultVOUtil.convert(hhyUniteMallProduct, HhyUniteMallProductVO.class);
            return ResultVOUtil.success(hhyProductVOOutput);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error(1, "创建失败，请稍后重试");
        }
    }

    @POST
    @Path("/uniteMallProduct/down")
    @Produces(MediaType.APPLICATION_JSON)
    public ResultVO downProduct(HhyUniteMallProductVO hhyProductVO) {
        try {
            // 鉴权
            HhySeller hhySeller = sellerWebsiteAuthorizeHandler.getHhySeller(httpServletRequest);
            if (hhySeller == null) {
                return sellerWebsiteAuthorizeHandler.createNoAuthorizedResultVO(httpServletResponse);
            }
            String productId = hhyProductVO.id;
            HhyProduct hhyUniteMallProduct = hhyUniteMallProductService.findOne(productId);
            if (!hasRight(hhySeller, hhyUniteMallProduct)) {
                return ResultVOUtil.error("不能下架其他商家的商品");
            }
            // 业务
            hhyUniteMallProduct.status = HhyProduct.statusDown;
            hhyUniteMallProduct = hhyUniteMallProductService.put(hhyUniteMallProduct);
            HhyUniteMallProductVO hhyProductVOOutput = ResultVOUtil.convert(hhyUniteMallProduct, HhyUniteMallProductVO.class);
            return ResultVOUtil.success(hhyProductVOOutput);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error(1, "创建失败，请稍后重试");
        }
    }

}
